package com.frontend.screens;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import com.data.documents.Invoice;
import com.data.documents.TreatmentRecord;
import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.users.User;

/**
 * This screen allows Nurses, Doctors, and Admins to create an invoice for a patient
 * 
 * @author Elizabeth
 *
 */
public class CreateInvoiceScreen extends Screen {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8251726846234301752L;
	private Screen prevScreen;
	private User patient;
	private Invoice invoice;
	private JLabel message;
	private JButton back, submit;
	private JCheckBox[] checkBoxes = new JCheckBox[17];
	private String[] checkBoxNames = { "Flu Vaccine", "Tetnus Vaccine",
			"MMR Vaccine", "Hepatitis A Vaccine", "Steroid Vaccine",
			"Urinalysis(BUN)", "Urinalysis (Drug Test)", "CBC", "Cholesterol",
			"HIV", "Glucose", "Adrenal", "EKG", "MRI", "Office Base Visit",
			"Phone Consult", "Annual Physical" };
	private String selectedItems;
	private TreatmentRecord tr;

	/**
	 * Creates a new create invoice screen so that the users can select what 
	 * procedures were performed and properly bill the patient
	 * 
	 * @param window
	 * @param invoice
	 * @param patient
	 * @param tr
	 * @param prevScreen
	 */
	public CreateInvoiceScreen(DoctorsOfficeClient window, Invoice invoice,
			User patient, TreatmentRecord tr, Screen prevScreen) {
		super(window);
		this.invoice = invoice;
		this.patient = patient;
		this.prevScreen = prevScreen;
		this.tr = tr;
		setLayout(null);
		
		InvoiceListener il = new InvoiceListener();
		
		message = new JLabel("Pick Operations to add to Invoice:");
		List<String> bill = invoice.getBill();
		//make the checkboxes
		for(int i = 0; i<checkBoxes.length; i++){
			checkBoxes[i] = new JCheckBox(checkBoxNames[i]);
			checkBoxes[i].setOpaque(false);
			add(checkBoxes[i]);
			if(bill != null){
				for(int j = 0; j<bill.size(); j++){
					if(bill.get(j).equals(checkBoxNames[i])){
						checkBoxes[i].setSelected(true);
						
					}
				}
			}
		}
		
		add(message);
		
		back = new JButton("Back");
		back.setOpaque(false);
		back.addActionListener(il);
		submit = new JButton("Submit");
		submit.setOpaque(false);
		submit.addActionListener(il);
		
		add(back);
		add(submit);
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			int width = getWidth(); 
			int height = getHeight();
			Insets insets = getInsets();
			int curY = height / 15;
			
			message.setFont(new Font("Arial", Font.BOLD, height / 15));
			FontMetrics fm = g.getFontMetrics(new Font("Arial", Font.BOLD,
					getHeight() / 15));
			int textWidth = (int) fm.getStringBounds(message.getText(), g)
					.getWidth();
			message.setBounds(width / 2 + insets.left - textWidth / 2, curY,
					width, height / 15);
			
			curY += 2 * height / 15;
			int i = 0;
			for(; i < 5; i++){
				checkBoxes[i*3].setFont(new Font("Arial", Font.BOLD, height / 25));
				checkBoxes[i*3+1].setFont(new Font("Arial", Font.BOLD, height / 25));
				checkBoxes[i*3+2].setFont(new Font("Arial", Font.BOLD, height / 25));
				checkBoxes[i*3].setBounds(width / 2 + insets.left - width / 3 - width / 6, curY, width / 3, height / 20);
				checkBoxes[i*3+1].setBounds(width / 2 + insets.left - width / 6, curY, width / 3, height / 20);
				checkBoxes[i*3+2].setBounds(width / 2 + insets.left + width / 6, curY, width / 3, height / 20);
				curY += height/20;
				curY += height/15;
			}
			
			checkBoxes[15].setFont(new Font("Arial", Font.BOLD, height / 25));
			checkBoxes[16].setFont(new Font("Arial", Font.BOLD, height / 25));
			checkBoxes[15].setBounds(width / 2 + insets.left - width / 30 - width / 4, curY, width / 4, height / 20);
			checkBoxes[16].setBounds(width / 2 + insets.left + width / 30, curY, width / 4, height / 20);
			
			curY += height / 20;
			curY += height / 15;
			
			submit.setFont(new Font("Arial", Font.BOLD, height / 25));
			back.setFont(new Font("Arial", Font.BOLD, height / 25));
			submit.setBounds(width / 2 + insets.left - width / 30 - width / 5, curY, width / 5, height / 20);
			back.setBounds(width / 2 + insets.left + width / 30, curY, width / 5, height / 20);
			
			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}
	
	
	private class InvoiceListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == back){
				window.changeScreen(prevScreen);
				prevScreen.revalidate();
				prevScreen.repaint();
			}
			else if(e.getSource() == submit){
				for(int i = 0; i<checkBoxes.length; i++){
					if (checkBoxes[i].isSelected()){
						if (selectedItems != null)
							selectedItems = selectedItems + ";" + checkBoxNames[i];
						else
							selectedItems = checkBoxNames[i];
					}
				}
				invoice.setBillItems(selectedItems);
				tr.setInvoice(invoice);
				System.out.println(selectedItems);
				try {
					window.getNet().updateDocument(invoice,patient.getUsername());
				} catch (ConnectionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}
}



